package com.sky.service.impl;

import static com.sky.constant.DefaultConstant.DEFAULT_MODEL;

import com.fasterxml.jackson.databind.ser.Serializers.Base;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Transaction;
import com.sky.exception.BaseException;
import com.sky.exception.PredictError;
import com.sky.mapper.GModelMapper;
import com.sky.result.PageResult;
import com.sky.service.GModelService;
import com.sky.utils.ModelUtils;
import com.sky.vo.GModelInfoVO;
import java.time.Duration;
import java.time.LocalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class GModelServiceImpl implements GModelService {

  @Autowired
  private GModelMapper gModelMapper;
  @Override
  public PageResult pageQueryModels(Integer pageNum, Integer pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    Page<GModelInfoVO> page = gModelMapper.modelsPageModelsQuery();
    return new PageResult(page.getTotal(), page.getResult());
  }

  @Override
  public PageResult pageQueryRecords(Integer pageNum, Integer pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    int guserId= BaseContext.getCurrentId();
    Page<Transaction> page = gModelMapper.recordsPageModelsQuery(guserId);
    return new PageResult(page.getTotal(), page.getResult());

  }

  @Override
  @Transactional
  public void deleteRecord(Integer tranId) {
    // 判断是否是这个人的id
    int currentId = BaseContext.getCurrentId();
    Transaction transaction = gModelMapper.selectRecord(currentId, tranId);
    if(transaction == null){
      throw new BaseException(MessageConstant.RECORD_NOT_EXISTS);
    }else{
      gModelMapper.deleteTransaction(currentId, tranId);
    }
  }

  @Override
  public String selectRecord(int tranId) {
    int currentId = BaseContext.getCurrentId();
    Transaction transaction = gModelMapper.selectRecord(currentId, tranId);
    if(transaction == null){
      throw new BaseException(MessageConstant.RECORD_NOT_EXISTS);
    }else{
      return transaction.getResultPath();
    }
  }

  @Override
  public PageResult pageQueryRecordsLimit(Integer pageNum, Integer pageSize, String fileName) {
    PageHelper.startPage(pageNum, pageSize);
    int guserId= BaseContext.getCurrentId();
    Page<Transaction> page = gModelMapper.recordsPageModelsQueryLimit(guserId,fileName);
    return new PageResult(page.getTotal(), page.getResult());

  }

  @Override
  @Transactional
  public int predictModels(String fileName, float limit) {
    LocalDateTime startTime = LocalDateTime.now();
    // 预测数据
    ModelUtils.predictData(fileName, limit);
    // 判断其是否写入成功
    if (!ModelUtils.checkFileExist(ModelUtils.getResFileName(fileName))) {
      throw new PredictError(MessageConstant.PREDICT_ERROR);
    }
    LocalDateTime endTime = LocalDateTime.now();

    int gUserId = BaseContext.getCurrentId();

    // 获取样本数量
    Integer sampleCount=ModelUtils.countLines(ModelUtils.originDataPath + fileName);
    Transaction transaction = Transaction.builder()
        .startTime(startTime)
        .endTime(endTime)
        .guserId(gUserId)
        .resultPath(ModelUtils.getResFileName(fileName))
        .modelId(DEFAULT_MODEL)
        .countSamples(sampleCount)
        .build();

    // 写入数据库
    gModelMapper.insertRecord(transaction); // 传入transaction对象作为参数
    return transaction.getTranId();
  }



}
